package it.getta.vicinometro.popup;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import com.google.android.maps.GeoPoint;

import it.getta.vicinometro.ActivityCallBack;
import it.getta.vicinometro.R;
import it.getta.vicinometro.scrollpicker.AbstractSlotView;
import it.getta.vicinometro.scrollpicker.ScrollPickerView;
import it.getta.vicinometro.scrollpicker.ScrollPickerView.OnValueChangeListener;
import it.getta.vicinometro.utils.Utils;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SetAlarmPopup extends VicinometroPopup {

//	public static final int RADIUS_MIN_VALUE = 30;
	private GeoPoint mLocation;
	private ScrollPickerView mScrollPicker;
	private TextView mRadiusValueLabel;
	private int mRadiusValue;
	
	public SetAlarmPopup(Context context, ActivityCallBack activityCallBack,
			GeoPoint location) {
		super(context, activityCallBack, R.layout.set_alarm_popup);
		mLocation = location;
	}
	
	@Override
	public void showPopup() {
		super.showPopup();
		Button ok = (Button)mPopupRootView.findViewById(R.id.set_alarm_popup_ok);
		ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mActivityCallBack.addAlarmMarker(mLocation, mRadiusValue, true);
				mActivityCallBack.getMapView().invalidate();
				dismiss();
			}
		});
		Button cancel = (Button)mPopupRootView.findViewById(R.id.set_Alarm_popup_cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		mRadiusValueLabel = (TextView)mPopupRootView.findViewById(R.id.set_alarm_popup_radius_value);
		mScrollPicker = (ScrollPickerView)mPopupRootView.findViewById(R.id.set_alarm_popup_scroll);
		mScrollPicker.addSlot(generatePickerThousandsValues(), 1, ScrollPickerView.ScrollType.Ranged);
		mScrollPicker.addSlot(generatePickerHundredsValues(), 1, ScrollPickerView.ScrollType.Ranged);
		mScrollPicker.setOnValueChangeListener(new OnValueChangeListener() {
			
			@Override 
			public void onValueChange(List<String> slotValues) {
				StringBuilder sbuilder = new StringBuilder(""); 
				for(String s: slotValues) {
					sbuilder.append(s);
				} 
				Utils.hardLong("value : " + sbuilder.toString());
				
				if(Integer.parseInt(sbuilder.toString()) == 0) {
					AbstractSlotView asv = mScrollPicker.getSlot(1);
					asv.scrollToLabelIndexAndStop(1);
				}
				
				updateRadiusValue(sbuilder.toString());
				
			}
		});
		mScrollPicker.setSlotIndexByScroll(1, 1);
		
		show(mActivityCallBack.getPopupAnchor());
	}
	
	private void updateRadiusValue(String value) {
		mRadiusValue = Integer.parseInt(value);
		DecimalFormat formatter = new DecimalFormat("#####");
		String formattedValue = formatter.format(mRadiusValue);
		mRadiusValueLabel.setText(formattedValue);
	}

	private String[] generatePickerThousandsValues() {
		String[] values = new String[30];
		for(int i=0; i<values.length; i++) {
			values[i] = "" + i;
		}
		return values;
	}
	
	private String[] generatePickerHundredsValues() {
		String[] values = new String[50];
		for(int i=0; i<values.length; i++) {
			values[i] = threeDigitsFormat(i * 20);
		}
		return values;
	}
	
//	private String[] generatePickerHundredsValuesFromMinValue() {
//		String[] values = new String[100];
//		for(int i=0; i<values.length; i++) {
//			values[i] = threeDigitsFormat(i * 10);
//		}
//		String[] s = values;
//		return values;
//	}
	
	private String threeDigitsFormat(int input) {
		NumberFormat formatter = new DecimalFormat("000");
		return formatter.format(input);
	}
	
}
